package org.example.ai_chat.interceptor;


import io.swagger.v3.oas.annotations.Hidden;
import org.example.ai_chat.util.CustomException;
import org.example.ai_chat.util.Result;
import org.example.ai_chat.util.ResultCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常拦截器，在这里捕获异常
 */
@RestControllerAdvice
@Hidden // 避免拦截Knife4j资源
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(CustomException.class)
    public Result handleCustomException(CustomException e) {
        logger.error(e.getMessage(), e);
        return Result.build(null, e.getCode(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        logger.error(e.getMessage(), e);
        return Result.build(null, ResultCodeEnum.SERVICE_ERROR);
    }

}
